import { defineStore } from 'pinia';
import { messageFn } from '../../utils';

import {
    getLateFeeListPage,//业务综合办理-违约金减免-获取用水项目违约金
    lateFeeReduced,//业务综合办理-违约金减免
    lateFeeReducedHandle,//违约金减免--计算--后端接口计算
} from '/@/api/comprehensive/special';

import { useLoading } from '/@/components/Loading';

const [openFullLoading, closeFullLoading] = useLoading({
    tip: '加载中...',
});


interface LateFeeFace {
    LateFeeList: [];
    CalcResult: {};
    total: number;
    pageInfo: object;
    storeBreachMoney: number; //减免违约金额
}

export const LateFeeFaceListStore = defineStore({
    id: 'LateFeeFace-state',
    state: (): LateFeeFace => ({
        LateFeeList: [], //获取到的违约金列表数据
        CalcResult : {}, //后端返回的计算结果
        total: 0,//总页数
        pageInfo: {
            //分页
            page_no: 1,
            page_size: 10,
        },
        storeBreachMoney: 0//减免违约金额
    }),
    getters: {

    },
    actions: {
        setLateFeeList(list: []) {
            //获取到的列表数据
            this.LateFeeList = list ? list : [];
        },
        setCalcResult(obj:object) {
            //后端返回的计算结果
            this.CalcResult = obj ? obj : [];
        },
        setTotal(total: number) {
            console.log('setTotal----', total);
            //总页数
            this.total = total;
        },
        setPageInfo(pageParams: object) {
            //设置页面数据
            this.pageInfo = pageParams;
        },
        setStoreBreachMoney(num: number) {
            console.log('setStoreBreachMoney----', num);
            this.storeBreachMoney = num;
        },

        /**
         * @description: getLateFeeListPage 分页请求违约金列表数据
         */
        async getLateFeeListAction(params: object) {
            //分页请求数据
            openFullLoading()
            try {
                const getLateFeeList = await getLateFeeListPage(params);
                if (getLateFeeList.code === 0) {
                    this.setLateFeeList(getLateFeeList.data);
                    this.setTotal(getLateFeeList.total);
                    return getLateFeeList.data;
                } else {
                    messageFn('error', getLateFeeList.msg);
                }
            } finally {
                closeFullLoading()
            }
        },

        /**
  * @description: lateFeeReduced //业务综合办理-违约金减免
  */
        async lateFeeReducedAction(params: object) {
            openFullLoading()
            try {
                const lateFee = await lateFeeReduced(params);
                if (lateFee.code === 0) {
                    messageFn('success', lateFee.msg);
                    // this.getLateFeeListAction(this.pageInfo);
                    return true;
                } else {
                    messageFn('error', lateFee.msg);
                    return false;
                }
            } finally {
                closeFullLoading()
            }
        },
        /**
  * @description:  lateFeeReducedHandle //违约金减免--计算--后端接口计算
  */
        async lateFeeReducedHandleAction(params: object) {
            openFullLoading()
            try {
                const lateFeeReduced = await lateFeeReducedHandle(params);
                if (lateFeeReduced.code === 0) {
                    // messageFn('success', lateFeeReduced.msg);
                    this.setCalcResult(lateFeeReduced.data);
                    return lateFeeReduced.data;
                } else {
                    messageFn('error', lateFeeReduced.msg);
                    return false;
                }
            } finally {
                closeFullLoading()
            }
        },

    },
});
